﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace 学生信息管理系统
{
    public partial class ManagerTMessage : Form
    {
        public ManagerTMessage()
        {
            InitializeComponent();
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            string cn_str = pub.CN_STR;
            OleDbConnection cn = new OleDbConnection(cn_str);              //建立数据库连接
            cn.Open();
            string select = "SELECT   密码   FROM      教师登录   WHERE   (用户名 = '" + txtNo.Text + "')";
            OleDbCommand cmd = new OleDbCommand(select, cn);                 //建立数据库命令
            OleDbDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                this.txtPassword.Text = reader["密码"].ToString();
                reader.Close();
            }
            cn.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string cn_str = pub.CN_STR;
            OleDbConnection cn = new OleDbConnection(cn_str);              //建立数据库连接
            cn.Open();
            string select = "UPDATE  教师登录   SET         密码 =" + txtPassword.Text + "    WHERE   (教师登录.用户名 = '" + txtNo.Text + "')";
            OleDbCommand cmd = new OleDbCommand(select, cn);                 //建立数据库命令
            OleDbDataReader reader = cmd.ExecuteReader();
            MessageBox.Show("修改密码成功！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);   
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            string cn_str = pub.CN_STR;
            OleDbConnection cn = new OleDbConnection(cn_str);              //建立数据库连接
            try
            {
                cn.Open();
                string select = "INSERT INTO 教师登录                 (用户名, 密码)   VALUES   ('" + txtNo.Text + "', '" + txtPassword.Text + "')";
                OleDbCommand cmd = new OleDbCommand(select, cn);                 //建立数据库命令
                OleDbDataReader reader = cmd.ExecuteReader();
                MessageBox.Show("添加成功！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                reader.Close();
                cn.Close();
            }

            catch (Exception ex)
            {
                MessageBox.Show("添加失败，已有重复数据！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            finally
            {
                cn.Close();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            string cn_str = pub.CN_STR;
            OleDbConnection cn = new OleDbConnection(cn_str);              //建立数据库连接
            cn.Open();
            string select = "DELETE FROM 教师登录   WHERE   (教师登录.用户名 = '" + txtNo.Text + "')";
            OleDbCommand cmd = new OleDbCommand(select, cn);                 //建立数据库命令
            OleDbDataReader reader = cmd.ExecuteReader();
            MessageBox.Show("删除成功！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            reader.Close();
            this.Close();
        }
    }
}
